﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.ConnectionUI;

namespace KeelKit.Dialogs
{
    public partial class frmSlnConfig : Form
    {
        public frmSlnConfig()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                KeelExt ke = Kit.SlnKeel;
                if (ke == null)
                {
                    ke = new KeelExt();
                }
                #region 数据连接
                ke.ConnectString = bcsCs.ConnectionString;
                if (bcsCs.DataSource != null)
                {
                    ke.DataSourceName = bcsCs.DataSource.Name;
                }
                if (bcsCs.DataProvider != null)
                {
                    ke.ProviderName = bcsCs.DataProvider.Name;
                }
                #endregion

                #region DAL相关
                ke.DALProjectName = cbxDALPjt.ProjectName;
                //Kit.GetProjectDAL().ProjectItems
                #endregion

                #region Model 相关
                ke.ModelProjectName = cbxModelPjt.ProjectName;
                ke.ModelGenerateMore = chkGenMore.Checked;
                ke.ModelAddMore = chkAddMore.Checked;
                ke.ModelForUI = chkModelForUI.Checked;
                if (rbDotNetAndDotNet.Checked)
                {
                    ke.ModelProjectKind = KeelExt.ModelKind.DotNetAndDotNet;
                }
                else if (rbDotNetAndC.Checked)
                {
                    ke.ModelProjectKind = KeelExt.ModelKind.DotNetAndC;
                }
                else
                {
                    ke.ModelProjectKind = KeelExt.ModelKind.DotNetAndDotNet;
                }


                ke.ModelForCPath = txtPath.Text;
                #endregion


                #region UI相关
                ke.UIAddForm = chkUIAddForm.Checked;
                ke.UIQueryForm = chkQueryForm.Checked;
                ke.UIPrinterForm = chkPrinter.Checked;
                ke.UIModifyForm = chkModifyForm.Checked;
                ke.UIProjectName = cbxUiPjt.ProjectName;
                #endregion

                Kit.SlnKeel.ComponentProjectName = cbxCmpntPjt.ProjectName;
                Kit.SlnKeel = ke;
                DialogResult = DialogResult.OK;
                Kit.AddKeelRefToProject(Kit.GetProjectModel());
                Kit.AddKeelRefToProject(Kit.GetProjectDAL());
                Kit.AddKeelRefToProject(Kit.GetProjectByName(Kit.SlnKeel.UIProjectName));
                Kit.AddProjectRefToProject(Kit.GetProjectModel(), Kit.GetProjectDAL());
                Kit.AddProjectRefToProject(Kit.GetProjectDAL(), Kit.GetProjectByName(Kit.SlnKeel.UIProjectName));


                this.Close();
            }
            catch (Exception ex)
            {

                Common.ShowInfo(ex.Message);
            }

        }

        private void frmSlnConfig_Load(object sender, EventArgs e)
        {
            if (Kit.SlnKeel == null) return;
            tabMain.SelectTab(tpCommon);
            try
            {
                #region 数据连接
                bcsCs.ConnectionString = Kit.SlnKeel.ConnectString;
                bcsCs.DataProvider = Kit.GetDataProviderByName(Kit.SlnKeel.ProviderName);
                bcsCs.DataSource = Kit.GetDataSourceByName(Kit.SlnKeel.DataSourceName);
                #endregion

                #region DAL相关
                cbxDALPjt.ProjectName = Kit.SlnKeel.DALProjectName;
                #endregion

                #region UI相关
                cbxUiPjt.ProjectName = Kit.SlnKeel.UIProjectName;
                chkUIAddForm.Checked = Kit.SlnKeel.UIAddForm;
                chkQueryForm.Checked = Kit.SlnKeel.UIQueryForm;
                chkPrinter.Checked = Kit.SlnKeel.UIPrinterForm;
                chkModifyForm.Checked = Kit.SlnKeel.UIModifyForm;
                #endregion

                cbxCmpntPjt.ProjectName = Kit.SlnKeel.ComponentProjectName;

                #region Model 相关
                chkModelForUI.Checked = Kit.SlnKeel.ModelForUI;
                cbxModelPjt.ProjectName = Kit.SlnKeel.ModelProjectName;
                chkGenMore.Checked = Kit.SlnKeel.ModelGenerateMore;
                chkAddMore.Checked = Kit.SlnKeel.ModelAddMore;

                switch (Kit.SlnKeel.ModelProjectKind)
                {
                    default:
                    case KeelExt.ModelKind.DotNetAndDotNet:
                        rbDotNetAndDotNet.Checked = true;
                        rbDotNetAndC.Checked = false;
                        break;
                    case KeelExt.ModelKind.DotNetAndC:
                        rbDotNetAndDotNet.Checked = false;
                        rbDotNetAndC.Checked = true;
                        break;
                }
                txtPath.Text = Kit.SlnKeel.ModelForCPath;
                #endregion
            }
            catch (Exception ex)
            {
                Common.ShowInfo(ex.Message);
            }


        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnSelectPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择存放目录:";
            fbd.SelectedPath = txtPath.Text;
            DialogResult dr = fbd.ShowDialog(this);
            if (dr == DialogResult.OK)
            {
                txtPath.Text = fbd.SelectedPath;
            }
        }

    }
}
